package com.gzc.pm.resource
{
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.utils.Dictionary;
	
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	
	[Event(name='complete', type='flash.events.Event')]
	public class MyResource extends BaseResource
	{
		
		private static var _instance:MyResource;
		
		public function MyResource(singletoner:Singletoner)
		{
			if(!singletoner){
				throw new Error('Please use Resource.getInstance() to get instance!');
			}
		}
		
		public static function getInstance():MyResource{
			if(!_instance){
				_instance = new MyResource(new Singletoner());
			}
			return _instance;
		}
		
		private var _xmlLoader:HTTPService;
		private var _imageObjArray:Array = [];
		private var _swfObjArray:Array = [];
		
		private var _loader:Loader;
		private var _imgLoadCount:int = 0;
		private var _swfLoadCount:int = 0;
		public static var imageDict:Dictionary = new Dictionary();
		public static var swfDict:Dictionary = new Dictionary();
		
		override public function load(urlPrefix:String=''):void{
			super.load(urlPrefix);
			
			_xmlLoader = new HTTPService();
			_xmlLoader.url = "assets/pm/xml/resource.xml";
			_xmlLoader.resultFormat = "e4x";
			_xmlLoader.addEventListener(ResultEvent.RESULT, onXmlLoaded);
			_xmlLoader.send();
			
			function onXmlLoaded(e:ResultEvent):void{
				var xml:XML = e.result as XML;
				var imageXmlList:XMLList = xml.images.image;
				var swfXmlList:XMLList = xml.swfs.swf;
				for each(var imageXml:XML in imageXmlList){
					var obj:Object = new Object();
					obj.name = imageXml.@name;
					obj.source = imageXml.@source;
					_imageObjArray.push(obj);
				}
				for each(var swfXml:XML in swfXmlList){
					var swfobj:Object = new Object();
					swfobj.name = swfXml.@name;
					swfobj.source = swfXml.@source;
					_swfObjArray.push(swfobj);
				}
				_resTotalCount = _imageObjArray.length+_swfObjArray.length;
				loadImages();
			}
		}
		
		public function loadImages():void{
			if(_imageObjArray.length>0){
				_loader = new Loader();
				_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
				loadImage(0);
			}else{
				loadSwfs();
			}
		}
		
		private function loadImage(index:int):void{
			var obj:Object = _imageObjArray[index] as Object;
			var src:String = obj.source;
			_loader.load(new URLRequest(_urlPrefix+src));
			trace(_urlPrefix+src);
		}
		private function onImageLoaded(e:Event):void{
			var loaderInfo:LoaderInfo = e.currentTarget as LoaderInfo;
			var name:String = _imageObjArray[_imgLoadCount].name;
			imageDict[name] = loaderInfo.content as Bitmap;
			
			_imgLoadCount++;
			increaseLoaded();
			if(_imgLoadCount < _imageObjArray.length){
				loadImage(_imgLoadCount);
			}else{//图片加载完毕
				loadSwfs();
			}
		}
		
		public function loadSwfs():void{
			if(_swfObjArray.length>0){
				_loader = new Loader();
				_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onSwfLoaded);
				loadSwf(0);
			}else{
				dispatchEvent(new Event(Event.COMPLETE));
			}
		}
		private function loadSwf(index:int):void{
			var obj:Object = _swfObjArray[index] as Object;
			var src:String = obj.source;
			_loader.load(new URLRequest(_urlPrefix+src));
		}
		private function onSwfLoaded(e:Event):void{
			var loaderInfo:LoaderInfo = e.currentTarget as LoaderInfo;
			var name:String = _swfObjArray[_swfLoadCount].name;
			swfDict[name] = loaderInfo.content as MovieClip;
			
			_swfLoadCount++;
			increaseLoaded();
			if(_swfLoadCount < _swfObjArray.length){
				loadSwf(_swfLoadCount);
			}else{//SWF加载完毕
				dispatchEvent(new Event(Event.COMPLETE));
			}
		}
		
	}
}

internal class Singletoner{}